<?php

namespace app\admin\controller;

use app\admin\model\News as Newss;
use app\admin\model\Tags;
use app\admin\validate\News as NewssValidate;
use think\Request;

class News extends Common
{

    public function initialize()
    {
        parent::initialize();
        $this->m = new Newss;   //别名：避免与控制名冲突
        $this->v=new NewssValidate;   //创建一个验证类
    }

    public function index()
    {
        $dataList = $this->m->paginate(10);
        $this->assign('dataList', $dataList);
        return $this->fetch();
    }

    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $result = $this->m->create($data);
            $tags=Tags::uniqueSave($data['tags']);
            $result->tags()->attach(array_column($tags,'id'));
            if ($result) {
                return xjson();
            } else {
                return xjson(404);
            }
        }
        $newsCategory= new \app\admin\model\NewsCategory();
        $treeList = $newsCategory->tree();
        $this->assign('treeList', $treeList);
        return $this->fetch();
    }

    public function edit($id)
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (!$this->v->scene('edit')->check($data)) {
                return xjson(404, $this->v->getError());
            }
            $newss = Newss::find($id);
            $result = $newss->save($data);
            //先删除
            $newss->tags()->detach();
            //再添加
            if(isset($data['tags'])){
                $tags=Tags::uniqueSave($data['tags']);
                $newss->tags()->attach(array_column($tags,'id'));
            }
            if ($result) {
                return xjson();
            } else {
                return xjson( 404);
            }
        } else {
            $data = $this->m->with('tags')->where('id',$id)->find();
            $this->assign('data', $data);
            $newsCategory= new \app\admin\model\NewsCategory();
            $treeList = $newsCategory->tree();
            $this->assign('treeList', $treeList);
            return $this->fetch();
        }
    }

    public function delete($id)
    {
        if (request()->isPost()) {
            //超级管理员不能删除
            if($id==1||strpos($id,'1,'!==false)){
                return xjson(404, '超级管理员不能删除');
            }
            $result = $this->m->where('id','in',$id)->delete();
            if ($result) {
                return xjson();
            } else {
                return xjson( 404, $this->m->getError());
            }
        }
    }

    /*
     * ajax更新部分数据
     */
    public function ajax($id)
    {
        return $this->_ajax($id);
    }

    public function tree($page = 1, $limit = 10)
    {

        $count=$this->m->count();
        $data=$this->m->with('newsCategory')->order('top desc,id desc')->page($page,$limit)->select();
        return json([
            'code'=>0,
            'msg'=>'查询成功',
            'count'=>$count,
            'data'=>$data
        ]);
    }
}